<?php

// $Id$


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_page_w_grid_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/grids/category_content/category_content_grid_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/category/tcms_category_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_template_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_category_dbc.class.php');


class Category_Content_WP extends TCMS_Admin_Page_W_Grid_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_category/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('category');
	}


	public function init() {
		parent::init();

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('unlink', Tangra_Parameter_Method::GET, 'unlink'), 'unlink_event');
	}


	protected function create_grid_ctrl() {
		$tvm = $this->get_tvm();

		$ret = new Category_Content_Grid_Ctrl('category_content', $tvm, Tangra_Parameter_Method::GET, $this->get_transit_var('category'), $this->_config->get_hidden_dir().'inc/');

		if (!$tvm->is_page_var_registered('category_content', 'path')) {
			$tvm->register_page_var('category_content', 'path');
			$tvm->register_page_var('category_content', 'online');

			$tmp = 0;
			$tvm->set_page_var('category_content', 'online', $tmp);

			$tmp2 = 0;
			$tvm->register_page_var('category_content', 'template');
			$tvm->set_page_var('category_content', 'template', $tmp2);

			$tmp3 = 2; // Normal pages only
			$tvm->register_page_var('category_content', 'sys_gen');
			$tvm->set_page_var('category_content', 'sys_gen', $tmp3);
		}

		$this->process_filter($tvm);

		$this->export_templates();

		return $ret;
	}


	private function process_filter($vm) {
		$context = $this->get__context();
		if ($context->exists_in_get('path')) {
			$path = $context->get_from_get('path');
			$vm->set_page_var('category_content', 'path', $path);
		}

		if ($context->exists_in_get('online')) {
			$online = $context->get_from_get('online');
			$vm->set_page_var('category_content', 'online', $online);
		}

		if ($context->exists_in_get('template')) {
			$template = $context->get_from_get('template');
			$vm->set_page_var('category_content', 'template', $template);
		}

		if ($context->exists_in_get('sys_gen')) {
			$sys_gen = $context->get_from_get('sys_gen');
			$vm->set_page_var('category_content', 'sys_gen', $sys_gen);
		}

		$path = $vm->get_page_var('category_content', 'path');
		$online = $vm->get_page_var('category_content', 'online');
		$template = $vm->get_page_var('category_content', 'template');
		$sys_gen = $vm->get_page_var('category_content', 'sys_gen');

		$this->export('path', $path);
		$this->export('online', $online);
		$this->export('template', $template);
		$this->export('sys_gen', $sys_gen);
	}


	private function export_templates() {
		$dbc = $this->get_dbc();

		$templates = TCMS_VPage_Template_DBC::load_templates_id_and_name($dbc);
		array_unshift($templates, array('id' => 0, 'name' => '---'));
		$this->export('templates', $templates);
	}


	protected function default_event($page_num = NULL) {
		$category_id = $this->get_transit_var('category');
		$category = new TCMS_Category_DBC();
		$dbc = $this->get_dbc();
		if (!$category->load_by_id($dbc, $category_id)) {
			throw new Tangra_Module_Exception('Cannot load TCMS_Category_DBC with id = '.$category_id);
		}

		$this->export('category', $category);

		return parent::default_event($page_num);
	}


	protected function unlink_event($vpage) {
		$dbc = $this->get_dbc();
		try {
			$dbc->start_trans();
			TCMS_VPage_Category_DBC::delete_for_vpage_and_category($dbc, $vpage, $this->get_transit_var('category'));
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		return $this->default_event();
	}
}